home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Pascal Super Library
/
Pascal Super Library (CW International)(1997).bin
/
DELPHI32
/
DEBUG
/
OBJTEST
/
COMMENTS.PAS
next >
Wrap
Pascal/Delphi Source File
|
1996-06-15
|
5KB
|
128 lines
unit Comments;
(*
This software is Freeware. Created by Theodore Kahn (tedkahn@interramp.com),
June 1996 for a talk presented at the North Bay Delphi Sig. (That's north of
San Francisco.) Comments welcome. Enjoy!
================================================================================
The only purpose of this unit is store comments. It contains no code.
================================================================================
BACKGROUND
================================================================================
The TStringList component is extremely useful in many situations. First, it
encapsulates a double-linked list of strings and adds a variety of methods for
manipulating those strings. For example, sorting, insert and delete. This
alone is of value. But, TStringList also provides for the association of an
object with each string in the list. The object cannot only contain data, but
can also include methods to operate on that data. In fact, TStringList is used
in almost every VCL component. (NOTE: In C++ and VB this functionality can be
found in Collections.)
This application demonstrates several of these features. It also shows how to
trap events that occur within the object from your program. Finally, the program
makes extensive use of typecasting.
The purpose of the object (TMyObjectClass) is to store a list of numbers
(contained in a TMemo) from which a mean can be calculated. Further, every time
the text in the memo changes, the mean is recalculated.
The object contains 1) a TMemo, 2) an event that gets fired when the memo
changes, 3) a field to store the mean, 4) a field to indicate if the mean needs
to be recalculated, 5) a read only property to access the mean, and 6) a
method to calculate the mean.
WHAT THE PROGRAM DOES
================================================================================
"Add New Object to StringList" button
--------------------------------------
When this button is clicked, it 1) creates a new instance of TMyObjectClass,
MyObject, 2) adds an item to the object`MyStringList, and 3) adds an item to
the listbox lstObjectString.
The item to MyStringList consists of the text of the textbox edtString and the
object MyObject. The item to lstObjectString is the same text as added to
MyStringList. lstObjectString allows us to access the various objects.
"Delete Object" button
------------------------
When this button is clicked, it 1) removes the association of the object to the
MyStringList, 2) destroys the object, that is, frees its memory, and 3)
removes the item from the lstObjectString.
"Calc" button
-------------
This button illustrates the fact that because the method CalcMean is public, it
can be accessed independent of the data in the object. In fact, that's why
CalcMean was made a separate function from GetMean. In this case, it calculates
the mean of the contents of memo1.
HOW THE MEMO CHANGE EVENT WORKS
================================================================================
When the text in the memo field changes, the following events occur in the order
shown:
1) Memo.OnChange event is fired, which has been equated to...
2) TMyObject.MyChange method which is executed causing...
3) TMyObject.OnDataChange event to be fired, which has been equated to...
4) TfrmMain.OnDataChange method which is executed, and contains code that...
5) Retrieves the value of the Mean property, which...
6) Causes the TMyObject.GetMean method to execute, which, if this started with
#2 above...
7) Causes the TMyObject.CalcMean to execute. Otherwise, the current value of
FMean is returned.
See how easy object programing is!
SUMMARY
================================================================================
While the amount of effort shown here is not warranted for this small amount of
data and calulations, it can easily be seen that the structure of the program
and object would not have to be changed much, even if the amount of data and
calculations were to be greatly increased.
It should also be noted that the program (the Main unit) itself mearly acts as
an interface to the data and calculations (the ObjUnit). In Object-Speak, this
means that the problem and interface domains are kept separate.
INSTRUCTIONS
================================================================================
1) Enter text into the editbox labeled "String Text."
2) Enter one or more numbers into the memo labeled "Numbers," memo1.
3) Click "Add New Object to StringList." The contents of the memo are copied
to the middle memo and the mean of the numbers is displayed below.
4) Repeat the above several times using different text and numbers.
5) Select an item from the listbox on the right. The memo associated with that
object is displayed in the middle memo, along with its mean below.
6) Click "Delete Object." The object associated with the selected item in the
listbox on the right is deleted.
7) Click "Calc." The mean of the numbers in the left memo are calculated and
displayed.
*)
interface
implementation
end.